package com.demo.servicelmpl;

import com.demo.bean.Task;
import com.demo.mapper.FixedEventsMapper;
import com.demo.mapper.ResultMapper;
import com.demo.mapper.TaskMapper;
import com.demo.service.RedisService;
import com.demo.service.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @author 李天航
 * 操作任务的实现类
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Resource
    TaskMapper taskMapper;
    @Resource
    FixedEventsMapper fixedEventsMapper;
    @Resource
    ResultMapper resultMapper;
    @Resource
    RedisService redisService;

    public static int MAX_TASK_NUM =20;

    @Override
    public LinkedHashMap<String, Object> addTask(Task task) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            if(taskMapper.getTaskCount(task)<MAX_TASK_NUM) {
                taskMapper.addTask(task);
                resultMap.put("status", 200);
                resultMap.put("msg", "添加成功");
            }
            else
            {
                resultMap.put("status",500);
                resultMap.put("msg","数量达到上限");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> deleteTask(int taskId, String userAccount) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            taskMapper.deleteTask(taskId);
            resultMapper.deleteFixedEventByTaskId(taskId);
            resultMap.put("status", 200);
            resultMap.put("msg", "删除成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> getTasks(Task task) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            resultMap.put("tasks",taskMapper.getTasksByUserAccountAndWeekNum(task));
            resultMap.put("status",200);
            resultMap.put("msg","获取成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> updateTask(Task task) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            taskMapper.updateTask(task);
            resultMap.put("status",200);
            resultMap.put("msg","修改成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }

    @Override
    public LinkedHashMap<String, Object> finishTask(int taskId) {
        LinkedHashMap<String, Object> resultMap=new LinkedHashMap<>();
        try {
            taskMapper.finishTask(taskId);
            resultMapper.finishFixedEventByResultId(taskId);
            resultMap.put("status",200);
            resultMap.put("msg","添加成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg",e.getMessage());
        }
        return resultMap;
    }
}
